package com.imoblife.brainwavestus.oldMedia;

import java.util.ArrayList;
import com.imoblife.brainwavestus.util.DebugUtil;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

public class MediaplayerFactory {

	private static BackgoundMediaPlayer mBackgoundMediaPlayer;

	private static ServiceConnection mConnection;

	private Context mContext;

	private static java.util.List<Running> runnings = new ArrayList<MediaplayerFactory.Running>();

	public interface Running {

		public void running(long current);

		public void start();

		public void pause();

		public void stop();
	}

	public void registerRunningListner(Running running) {

		if (runnings.indexOf(running) == -1) {
			runnings.add(running);
		}

	}

	public void unregisterRunningListner(Running running) {
		runnings.remove(running);
	}

	public MediaplayerFactory(Context context) {
		mContext = context;
	}

	Handler mRuningHandler;

	public void initializedMediaPlayer() {

		if (mBackgoundMediaPlayer == null) {

			mRuningHandler = new Handler() {

				public void handleMessage(Message msg) {

					DebugUtil.debug("time", "handleMessage" + "(Long) msg.obj="
							+ (Long) msg.obj);

					if (msg.what == 0) {
						for (Running item : runnings) {
							DebugUtil.debug("time", item + "handleMessage"
									+ "(Long) msg.obj=" + (Long) msg.obj);

							item.running((Long) msg.obj);
						}
					} else if (msg.what == -1) {
						for (Running item : runnings) {
							item.stop();
						}
					} else if (msg.what == -2) {
						for (Running item : runnings) {
							item.pause();
						}
					} else if (msg.what == 1) {
						for (Running item : runnings) {
							item.start();
						}
					}

				}

			};

			mConnection = new ServiceConnection() {

				public void onServiceDisconnected(ComponentName name) {
					mBackgoundMediaPlayer = null;
					DebugUtil.debug(MediaplayerFactory.class.getName(),
							"onServiceDisconnected");
				}

				public void onServiceConnected(ComponentName name,
						IBinder service) {
					DebugUtil.debug(MediaplayerFactory.class.getName(),
							"onServiceConnected");

					mBackgoundMediaPlayer = BackgoundMediaPlayer.Stub
							.asInterface(service);
					MediaPlayerService.setMassageHandler(mRuningHandler);
				}
			};
			try {
				Intent intent = new Intent(mContext, MediaPlayerService.class);
				mContext.startService(intent);
				mContext.bindService(intent, mConnection,
						Context.BIND_AUTO_CREATE);
			} catch (Exception e) {
				// TODO: handle exception
			}

		}

	}

	public boolean isInitialized() {

		return mBackgoundMediaPlayer != null;
	}

	public BackgoundMediaPlayer getBackgoundMediaPlayer() {

		return mBackgoundMediaPlayer;

	}

	public void destroyMediaPlayer() {

		try {
			mContext.unbindService(mConnection);
		} catch (Exception e) {
			mContext.stopService(new Intent(mContext, MediaPlayerService.class));
		}
		mBackgoundMediaPlayer = null;

		DebugUtil.debug(MediaplayerFactory.class.getName(),
				"destroyMediaPlayer");

	}

	public void clareListner() {
		runnings.clear();
	}

}
